package com.loong.coupon.handler;

import com.loong.common.enumerate.CouponSource;
import com.loong.common.enumerate.CouponStatus;
import com.loong.coupon.business.CouponBusiness;
import com.loong.coupon.business.UserCouponBusiness;
import com.loong.coupon.common.backend.param.CouponQueryParam;
import com.loong.coupon.model.Coupon;
import com.loong.coupon.model.UserCoupon;
import com.loong.global.dto.UserDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 处理用户优惠券业务的handler
 * @Author: LiuQun
 * @Date: 2021/1/8 9:11
 */
@Service
public class UserCouponHandler {

    @Resource
    private UserCouponBusiness userCouponBusiness;

    @Resource
    private CouponHandler couponHandler;

    @Resource
    private CouponBusiness couponBusiness;

    public void foreheadSaveUserCoupon(UserDTO userDTO){
        //根据参数查询平台赠送的所有优惠券
        CouponQueryParam queryParam = CouponQueryParam.builder()
                //优惠券来源是平台赠送
                .couponSource(CouponSource.PLATFORM_GIFT.getCode())
                //优惠券状态是审核已通过
                .status(CouponStatus.APPROVED.getCode())
                .build();
        List<Coupon> couponList = couponBusiness.findCouponByParam(queryParam);
        for (Coupon coupon : couponList) {
            //根据优惠券id查询优惠券的适用规则

        }


    }

    /**
     * 后台判断是否存在用户券关联记录
     * @param couponId 优惠券id
     * @return 是否存在
     */
    protected boolean nonexistentUserCoupon(Long couponId){
        boolean flag = false;
        List<UserCoupon> userCouponList = userCouponBusiness.findAllBy("couponId", couponId);
        if (CollectionUtils.isEmpty(userCouponList)) flag = true;
        return flag;
    }
}
